
import argopt


def get_include_path(opts):
    ret = []
    for module in opts['modules']:
        ret.append('#include "' + opts['path'] + '_unittest/' + module + '_unittest.h"')
    return ret


def get_unittest_function_call(opts):
    ret = []
    for module in opts['modules']:
        ret.append('    ' + module+'_unittest_execute( test_mode_release , time(NIL) );')
    return ret

if __name__ == "__main__":
    data = [{
        'name': 'modules',
        'type': type(list),
        'default': [],
        'comment': 'modules to unit test'
    }, {
        'name': 'path',
        'type': type(str),
        'default': "",
        'comment': 'path'
    },
        {
        'name': 'help',
        'type': type(bool),
        'default': False,
        'comment': 'main function construct tool '
    }]

    help_text = '''
     unittest_code_generator created by WPz.
       syntax:
             unittest_code_generator.py modules=
       example:
             unittest_code_generator.py modules=['control_pump','control_dcdc'] path = '"../control_pump'
     '''

    error, unknown, args, opts = argopt.parse(data, help_text)

    code = [
        '#ifndef UNIT_TEST',
        '#endif',
        '#include "../test/test.h"',
        '#include <time.h>',
        'int main(void)',
        '{',
        '#ifndef UNIT_TEST',
        '#endif',
        '',
        '    return EXIT_SUCCESS;',
        '}',
    ]
    include_cnt = 0
    for include_code in get_include_path(opts):
        code.insert(1, include_code)
        include_cnt += 1
    for include_code in get_unittest_function_call(opts):
        code.insert(7 + include_cnt, include_code)
        include_cnt += 1

    fp = open('../src/main/main.c', 'w+')
    for line in code:
        fp.write(line+'\n')
    fp.close()
